package com.wwwarehouse.carddesk.fragment.mytabfragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.carddesk.eventbus_event.CardDeskMyEmailChangeEvent;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.custom_widget.button.StateButton;
import com.wwwarehouse.common.tools.rsa.StringUtils;

import de.greenrobot.event.EventBus;

/**
 * author ：minifly
 * date: 2017/10/21
 * time: 10:52
 * desc: 信息页面修改用户名与邮箱页面
 */
public class MyMessageNameEmailFragment extends BaseFragment implements View.OnClickListener {

    private View v;
    private StateButton mDeskMyMessageChangebutton;
    private ElasticScrollView mElasticScroll;
    private TextView mDeskMyMessageEmailTxt,mPhoneTxt;
    private String email;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_desk_my_message_nameemail, container, false);
        initView(v);
        return v;

    }

    public void initView(View view) {

        mDeskMyMessageChangebutton = (StateButton) v.findViewById(R.id.desk_my_message_changebutton);
        mDeskMyMessageChangebutton.setOnClickListener(this);
        mElasticScroll = (ElasticScrollView) v.findViewById(R.id.elastic_scroll);
        mDeskMyMessageEmailTxt = (TextView) v.findViewById(R.id.desk_my_message_email_txt);
        mPhoneTxt = (TextView) v.findViewById(R.id.desk_my_message_phone_txt);

        mDeskMyMessageEmailTxt.setText("" + getString(R.string.taskdesk_my_message_email_pre) + getArguments().getString("email"));

        if(!StringUtils.isNullString(getArguments().getString("phone"))){
            mPhoneTxt.setText("" + getString(R.string.taskdesk_my_message_phone_pre) + getArguments().getString("phone").replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2"));
        }

        EventBus.getDefault().register(this);

        email = getArguments().getString("email");
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof MyMessageNameEmailFragment) {
            mActivity.setTitle(getString(R.string.taskdesk_my_changebinder));
        }
    }

    @Override
    public void requestDatas() {

    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.desk_my_message_changebutton) {

            Bundle bundle = new Bundle();
            bundle.putString("email","" + email);
            MyMessageChangeEmailFragment fragment = new MyMessageChangeEmailFragment();
            fragment.setArguments(bundle);
            pushFragment(fragment);

        }
    }

    public void onEventMainThread(Object event) {
        if(peekFragment() instanceof  MyMessageNameEmailFragment && event instanceof CardDeskMyEmailChangeEvent){
            CardDeskMyEmailChangeEvent event1 = (CardDeskMyEmailChangeEvent)event;
            mDeskMyMessageEmailTxt.setText("" + getString(R.string.taskdesk_my_message_email_pre) + event1.getMsg());//改动已经改了的邮箱
            email =  event1.getMsg();
        }

//        if(event instanceof BackListenerEvent){
//            BackListenerEvent event1 = (BackListenerEvent)event;
//            String message = event1.getMsg();
//            if (message.equals("PRUncollectedDetailFragment")) {
//                popFragment();
//            }
//        }

    }



    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
